package com.test.qnews.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.test.qnews.R;
import com.test.qnews.beans.DrawerBean;
import com.test.qnews.clicklistener.DrawerClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/10.
 */
public class DrawerAdapter extends RecyclerView.Adapter<DrawerAdapter.MyViewHolder>{

    private Context context;
    private List<DrawerBean> data;
    private DrawerClickListener drawerClickListener;

    public void setDrawerClickListener(DrawerClickListener drawerClickListener) {
        this.drawerClickListener = drawerClickListener;
    }

    public DrawerAdapter(Context context) {
        this.context = context;
        initData();
    }

    private void initData() {
        data = new ArrayList<>();
        data.add(new DrawerBean("关注",R.mipmap.love));
        data.add(new DrawerBean("收藏",R.mipmap.shoucang));
        data.add(new DrawerBean("设置",R.mipmap.set));
        data.add(new DrawerBean("问吧",R.mipmap.love));
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_drawer,parent,false);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;

    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        holder.optionTv.setText(data.get(position).getOption().toString());
        holder.imageIv.setImageResource(data.get(position).getImage());


        if (drawerClickListener != null){
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = holder.getLayoutPosition();
                    drawerClickListener.drawerOnClick(pos);
                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        TextView optionTv;
        ImageView imageIv;
        public MyViewHolder(View itemView) {
            super(itemView);
            optionTv = (TextView) itemView.findViewById(R.id.tv_drawer_item);
            imageIv = (ImageView) itemView.findViewById(R.id.iv_drawer_item);
        }
    }

}
